package com.egg.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import com.egg.action.common.BaseAction;
import com.egg.common.ListToJsonUtil;
import com.egg.service.DeptService;
import com.egg.service.MenuService;

/**
 * @项目名称：egg   
 * @类名称：MenuAction   
 * @类描述：菜单（权限）   
 * @创建人：wangchenyang
 * @创建时间：2012-8-31 下午4:52:51   
 * @修改人：
 * @修改时间：   
 * @修改备注：   
 * @version
 */
@Namespace("/menu")
@ParentPackage(value = "ajax")
public class MenuAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private List<Map<String,Object>> menuList;
	private List<Map<String,Object>> deptList;
	private String checkedMenuId;
	private boolean state=false;
	private String menuId;
	private String menuName;
	private String menuTree;
	private String menuUrl;
	private String roleId;
	private String paId;

	@Autowired
	MenuService menuService;
	@Autowired
	DeptService deptService;
	//所有菜单列表
	@Action(value="getAllMenu",results={@Result(name="success",location="/system/menu/listMenu.jsp")})
	public String getAllMenu(){
		String deptId=String.valueOf(getSession().getAttribute("deptId"));
		menuList=menuService.getAllMenu(deptId);
		return "success";
	}
	//跳转到增加页面
	@Action(value="preAddMenu",results={@Result(name="success",location="/system/menu/addOrUpdateMenu.jsp")})
	public String preAddMenu(){
		deptList=deptService.getAllDeptForSelect();
		return "success";
	}
	//增加、修改
	@Action(value="addOrUpdateMenu",results={@Result(name="redirect",type="redirect",location="getAllMenu.action")})
	public String addOrUpdateMenu(){
		String deptId=String.valueOf(getSession().getAttribute("deptId"));
		if(menuId!=null&&!"".equals(menuId)){
			menuService.updateMenu(menuName,menuUrl,deptId,paId,menuId);
		}else{ 
			menuService.addMenu(menuName,menuUrl,deptId,paId,new Date());
		}
		return "redirect";
	}
	//跳转到修改页面
	@Action(value="preUpdateMenu",results={@Result(name="success",location="/system/menu/addOrUpdateMenu.jsp")})
	public String preUpdateMenu(){
		menuList=menuService.getOneMenu(menuId);
		deptList=deptService.getAllDeptForSelect();
		return "success";
	}
	//删除
	@Action(value="deleteMenu",results={@Result(name="redirect",type="redirect",location="getAllMenu.action")})
	public String deleteMenu(){
		menuService.deleteMenu(menuId);
		return "redirect";
	}
	//获得树形数据
	@Action(value="queryTreeData",results={@Result(type="json")})
	public String queryTreeData(){
		String deptId=String.valueOf(getSession().getAttribute("deptId"));
		menuList=menuService.getMenuTreeByDeptId(deptId);
		menuTree=ListToJsonUtil.convertList2Json(menuList);
		return "success";
	}
	//展示树形菜单（带复选框）
	@Action(value="showTreeCheckBox",results={@Result(type="json")})
	public String showTreeCheckBox(){
		String deptId=String.valueOf(getSession().getAttribute("deptId"));
		menuList=menuService.getMenuTreeByDeptId(deptId);
		List<Map<String,Object>> selectList=menuService.getMenuByRoleId(roleId);
		menuTree=ListToJsonUtil.convertList2JsonForCheckBox(menuList,selectList);
		return "success";
	}
	//分配权限
	@Action(value="assignPower",results={@Result(type="json")})
	public String assignPower(){
		menuService.assignPower(roleId, checkedMenuId);
		state=true;
		return "success";
	}
	//获得权限菜单
	@Action(value="queryAuthMenu",results={@Result(type="json")})
	public String queryAuthMenu(){
		String userId=String.valueOf(getSession().getAttribute("userId"));
		menuList=menuService.getAuthMenu(userId);
		menuTree=ListToJsonUtil.convertList2JsonForUrl(menuList);
		return "success";
	}
	public List<Map<String, Object>> getMenuList() {
		return menuList;
	}
	public void setMenuList(List<Map<String, Object>> menuList) {
		this.menuList = menuList;
	}
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getMenuUrl() {
		return menuUrl;
	}
	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}
	@JSON(serialize = false)
	public MenuService getMenuService() {
		return menuService;
	}
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getMenuTree() {
		return menuTree;
	}
	public void setMenuTree(String menuTree) {
		this.menuTree = menuTree;
	}
	public List<Map<String, Object>> getDeptList() {
		return deptList;
	}
	public void setDeptList(List<Map<String, Object>> deptList) {
		this.deptList = deptList;
	}
	public String getPaId() {
		return paId;
	}
	public void setPaId(String paId) {
		this.paId = paId;
	}
	public String getCheckedMenuId() {
		return checkedMenuId;
	}
	public void setCheckedMenuId(String checkedMenuId) {
		this.checkedMenuId = checkedMenuId;
	}
	public String getRoleId() {
		return roleId;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	@JSON(serialize = false)
	public DeptService getDeptService() {
		return deptService;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	public boolean isState() {
		return state;
	}
	public void setState(boolean state) {
		this.state = state;
	}
	
}
